import org.neo4j.graphalgo.GraphAlgoFactory;
import org.neo4j.graphalgo.PathFinder;
import org.neo4j.graphalgo.WeightedPath;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.index.Index;
import org.neo4j.kernel.EmbeddedGraphDatabase;
import org.neo4j.kernel.Traversal;


public class PSDBV11 {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) {
		EmbeddedGraphDatabase neo4jdb = new EmbeddedGraphDatabase("neo4jtest");
		try{
			
			GraphCreator.createGraph(neo4jdb, neo4jdb.beginTx());
//			Transaction tx = neo4jdb.beginTx();
			Index<Node> index = neo4jdb.index().forNodes("nodes");
			
			{
				Node innsbruck = index.get("name", "Innsbruck").getSingle();
				Node graz = index.get("name","Graz").getSingle();
				System.out.println("Alle Pfade zwischen Innsbruck und Graz:");
				PathFinder<Path> finder = GraphAlgoFactory.allSimplePaths(Traversal.expanderForAllTypes(), 20);
				for (Path path : finder.findAllPaths(innsbruck, graz)) {
					for (Node node : path.nodes()) {
						String name = (String) node.getProperty("name");
						System.out.print(name + (name.equals("Graz") ? "" : " -> "));
					}
					System.out.println();
				} 
			}
			
			Node bergenz = index.get("name","Bergenz").getSingle();
			Node wien = index.get("name","Wien").getSingle();
			Node bischofshofen = index.get("name","Bischofshofen").getSingle();
			
			System.out.println("\nOptimale Route zwischen Bregenz und Wien via Bischofshofen:");
			PathFinder<WeightedPath> finder = GraphAlgoFactory.dijkstra(Traversal.expanderForAllTypes(), "distance");
			double distance = 0.0;
			WeightedPath path = finder.findSinglePath(bergenz, bischofshofen);
			distance += path.weight();
			for (Node node : path.nodes()) {
				String name = (String) node.getProperty("name");
				System.out.print(name + " -> ");
			}
			
			path = finder.findSinglePath(bischofshofen, wien);
			distance+=path.weight();
			for (Node node : path.nodes()) {
				String name = (String) node.getProperty("name");
				System.out.print((name.equals("Bischofshofen") ? "" : name) + (name.equals("Wien") || name.equals("Bischofshofen") ? "" : " -> "));
			}
			System.out.println(" //Laenge:"+distance);
			

		}catch (Exception e){
			e.printStackTrace();
		}finally{
			neo4jdb.shutdown();
		}
	}

}
